<?php
/*
Template Name: Contact
*/
?>

<?php get_header(); ?>
<?php 
$t_id = 0;
global $current_user;
get_currentuserinfo();

$aOptions = SofaSuppaStore::initOptions( false );
$send_form = ( strlen( $aOptions[ 'send_form' ] ) > 0 ) ? stripslashes( strip_tags( $aOptions[ 'send_form' ] ) ) : NULL;
?>

<!-- main content start -->

<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>


    <div class="castshadow grid_8 clearfix">
    
    <!-- title start -->

        <h1><?php the_title(); ?></h1>
        
    <!-- title end -->  
    
    <!-- content start -->
        
        <?php the_content(); ?>
        
    <!-- content end -->
    
    <!-- contact form start -->
                    
        <?php
		// form submitted
		if( isset( $_POST[ 'yps' ] ) && $_POST[ 'yps' ] == 'submitted' ) {
			$err_msg = '';
			// security error
			if( !wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) $err_msg .= '<h2>' . __( "Permissions Error!", "sofa_suppastore" ) . '</h2><p>' . __( "You don't have a permission to perform such action!", "sofa_suppastore" ) . '</p>';
			// name
			if( isset( $_POST[ 'yourname' ] ) && strlen( $_POST[ 'yourname' ] ) < 3 ) $err_msg .= '<h2>' . __( "Name Error!", "sofa_suppastore" ) . '</h2><p>' . __( "Name field is mandatory and can not contain less than 3 characters!", "sofa_suppastore" ) . '</p>';
			// message
			if( isset( $_POST[ 'message' ] ) && strlen( $_POST[ 'message' ] ) < 10 ) $err_msg .= '<h2>' . __( "Message Error!", "sofa_suppastore" ) . '</h2><p>' . __( "Message field is mandatory and can't contain less than 10 characters!", "sofa_suppastore" ) . '</p>';
			// validate human
			$sum = (int)$_POST[ 'spam' ];
			$v_1 = $_POST[ 'v_1' ]; 
			$v_1 = str_replace( 'sx', '', $v_1 );
			$v_2 = $_POST[ 'v_2' ]; 
			$v_2 = str_replace( 'sy', '', $v_2 );
			$sum_now = (int)$v_1 + (int)$v_2;
			if( $sum != $sum_now ) $err_msg .= '<h2>' . __( "Spam prevention Error!", "sofa_suppastore" ) . '</h2><p>' . __( "Sum is not correct, check it out once again!", "sofa_suppastore" ) . '</p>';
			// validate email
			if( !is_email( $_POST[ 'email' ] ) ) $err_msg .= '<h2>' . __( "Email Error!", "sofa_suppastore" ) . '</h2><p>' . __( "Email field is mandatory or unknown (incorrect) e-mail address format.", "sofa_suppastore" ) . '</p>';
			
			// test passed?
			if( $err_msg == '' ) {
		?>
        
		<?php
            //Who do you want to be the recipient of the contact form
			if( !$send_form ) {
            	$owner = get_userdata( 1 ); // to admin
            	$to = $owner->user_email;
			} else $to = $send_form;
        
            //This file is responsible for sending the email, you shouldn't need to edit it beyond here.
        
            //User info (DO NOT EDIT!)
            $name = stripslashes( $_POST[ 'yourname' ] ); //sender's name
            $email = stripslashes( $_POST[ 'email' ] ); //sender's email
            $entrysocial = stripslashes( $_POST[ 'entrysocial' ] ); // whatever
			$skype = stripslashes( $_POST[ 'skype' ] ); // skype gtalk
        
            //The subject
            $subject = '[ WEB Form - ' . get_bloginfo( 'name' ) . ' ]'; //Subject will include your blogs title for easy recognition.
            $message = stripslashes( $_POST[ 'message' ] );
        
            //The message you will receive in your mailbox
            //Each parts are commented to help you understand what it does exaclty.
            //YOU DON'T NEED TO EDIT IT BELOW BUT IF YOU DO, DO IT WITH CAUTION!
            
            // create proper headers so non-ascii characters are displayed properly
            $headers = "Content-Type: text/plain; charset=utf-8; format=flowed\r\n"
            . "Content-Transfer-Encoding: 8bit\r\n"
            . "MIME-Version: 1.0\r\n"
            . "From: $email\r\n"
            . "Reply-To: $email\r\n"
            . "Return-Path: $email\r\n";
        
            $msg  = "From : $name\r\n";  //add sender's name to the message
            $msg .= "e-Mail : $email\r\n";  //add sender's email to the message
            $msg .= "Subject : $subject\r\n\n"; //add subject to the message (optional! It will be displayed in the header anyway)
            $msg .= "---Message---\r\n$message\r\n\n";  //the message itself
        
            //Extras: User info (Optional!)
            //Delete this part if you don't need it
            //Display user information such as Ip address and browsers information...
            $msg .= "---User information---\r\n"; //Title
            $msg .= "User IP : " . $_SERVER[ "REMOTE_ADDR" ] . "\r\n"; //Sender's IP
            $msg .= "Phone/Cell : " . ( ( $entrysocial ) ? $entrysocial : 'Not available' ) . "\r\n"; //Sender's contact
			$msg .= "Skype/GoogleTalk : " . ( ( $skype ) ? $skype : 'Not available' ) . "\r\n"; //Sender's online
        
            if ( @mail( $to, $subject, $msg, $headers ) ) {
                echo '<h2>' . __( "Message sent successfully!", "sofa_suppastore" ) . '</h2><p>' . __( "Thanks", "sofa_suppastore" ) . ' <b>' . $name . '</b>, ' . __( "your message has been sent. We'll get back with an answer as soon as possible.", "sofa_suppastore" ) . '</p>';
            }
            else {
                echo '<h2>' . __( "Summary:", "sofa_suppastore" ) . '</h2><p>' . __( "Sorry", "sofa_suppastore" ) . ' <b>' . $name . '</b>, ' . __( "sending your message failed. Try again later!", "sofa_suppastore" ) . '</p>';
            }
        ?>
        
        <?php 
			} else {
				// test not passed
				echo '<p style="color: red;">' . __( "Your message is not delivered because of the following reason(s):", "sofa_suppastore" ) . '</p>';
				echo $err_msg;
			}
		} else { 
					$t_id = get_permalink( $post->ID );
		?>
                    
					<form id="contactform" method="post" action="<?php echo $t_id; ?>">
                    <p style="display: none;">
                    <?php wp_nonce_field( 'chck_ref_cc', 'sofa_suppastore_cc' ); ?>
                    </p>
						<p class="grid_4 alpha">
							<label for="yourname">* <?php _e( "Name", "sofa_suppastore" ); ?></label>
							<input type="text"  size="45" id="yourname" name="yourname" tabindex="444" />
						</p>

						<p class="grid_4 omega">
							<label for="email">* <?php _e( "E-mail address", "sofa_suppastore" ); ?></label>
							<input type="text" size="45" id="email" name="email" tabindex="445" />
						</p>
						<p class="grid_4 alpha">
							<label for="entrysocial"><?php _e( "Phone/Cell", "sofa_suppastore" ); ?></label>
							<input type="text" size="45" id="entrysocial" name="entrysocial" tabindex="446" />
						</p>
						<p class="grid_4 omega">
							<label for="entrysocial"><?php _e( "Skype/GoogleTalk", "sofa_suppastore" ); ?></label>
							<input type="text" size="45" id="skype" name="skype" tabindex="447" />
						</p>
						<p class="grid_8 alpha omega">
							<label for="message">* <?php _e( "Message", "sofa_suppastore" ); ?></label>
							<textarea rows="10" cols="52" id="message" name="message" tabindex="448"></textarea>
						</p>
                        <p class="grid_2 alpha">
                           <?php
						   $v_1 = rand( '1', '10' );
						   $v_2 = rand( '1', '10' );
						   ?>
	                       <label id="spamlabel" for="spam">* <?php echo $v_1 . ' + ' . $v_2 . ' = '; ?>
	                       <input type="text" name="spam" id="spam" size="2" value="" class="input" tabindex="449" />
                           </label>
                        </p>
                        <p style="display: none;">
						   <input type="hidden" name="v_1" value="sx<?php echo $v_1; ?>" />
						   <input type="hidden" name="v_2" value="sy<?php echo $v_2; ?>" />
                           <input type="hidden" name="yps" id="yps" value="submitted" />
                        </p>
                        <p style="display: none;">
                        <input type="hidden" value="" name="comment_post_ID"/>
                        </p>
						<p class="grid_6 omega textright">
							<button type="submit" value="Submit" id="submit" name="submit" tabindex="450"><?php _e( "SUBMIT", "sofa_suppastore" ); ?></button>
						</p>
					</form>
                    
        <?php
		}
		?>
       
	<?php endwhile; endif; ?>
    
    <!-- contact form end -->
    
    </div>
    
<!-- main content end -->

<!-- include sidebar start -->

    <?php get_template_part( 'sidebar_contact' ); ?>

<!-- include sidebar start -->

<?php get_footer(); ?>
		   